<template>
  <div class="apply-dialog">
    <div v-if="thisDiv === 1 || thisDiv === 2">
      <div class="title" style="display: flex;">
        <div style="flex: 1; text-align: left;padding-left: 30px;font-size: 25px;">
          {{ models.eventName }}
        </div>
        <div class="btn-list flex flex-row align-center">
          <el-button type="warning" size="mini" :loading="saveLoading" @click="saveAction">暂存</el-button>
          <el-button type="primary" size="mini" :loading="submitLoading" @click="submitAction">上报</el-button>
          <el-button type="info" size="mini" @click="handleClose">返回</el-button>
        </div>
      </div>
      <el-form
        ref="generateForm"
        label-suffix=":"
        size="small"
        :model="models"
        :rules="rules"
        label-position="right"
        label-width="130px"
      >
        <div >
          <title-info title="上报⼈信息" >
            <reporter-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="当事人信息" >
            <party-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="当事患者信息" >
            <patient-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="事件信息" >
            <event-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
        <div >
          <title-info title="错误发⽣阶段" >
            <div slot="left" style="display: flex; width: 100%; flex-wrap: wrap; ">
              <el-form-item  label="验⾎错误" prop="bloodTestArr">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.bloodTestArr">
                    <el-checkbox v-for="(item, index) in bloodTestList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                    <el-input
                      v-if="models.bloodTestArr && models.bloodTestArr.indexOf('382')  > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordHandle.bloodTestOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
              <div style="width: 100%"/>
              <el-form-item  label="备⾎错误(⾃开⽴输⾎医嘱⾄完成备⾎)" prop="bloodPrepareArr">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.bloodPrepareArr">
                    <el-checkbox v-for="(item, index) in bloodPrepareList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                    <el-input
                      v-if="models.bloodPrepareArr && models.bloodPrepareArr.indexOf('397')  > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordHandle.bloodPrepareOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
              <div style="width: 100%"/>
              <el-form-item  label="输⾎错误" prop="bloodInputArr">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.bloodInputArr">
                    <el-checkbox v-for="(item, index) in bloodInputList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                    <el-input
                      v-if="models.bloodInputArr && models.bloodInputArr.indexOf('404')  > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordHandle.bloodInputOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
            </div>
          </title-info>
        </div>
        <div >
          <title-info title="事后引起的输⾎反应" >
            <div slot="left" style="display: flex; width: 80%; flex-wrap: wrap; ">
              <el-form-item  label="反应类型" prop="bloodInputReactionArr">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.bloodInputReactionArr">
                    <el-checkbox v-for="(item, index) in bloodInputReactionList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                    <el-input
                      v-if="models.bloodInputReactionArr && models.bloodInputReactionArr.indexOf('424')  > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordHandle.bloodInputReactionOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
            </div>
          </title-info>
        </div>
        <div >
          <title-info title="处理措施" >
            <measure-two-info slot="left" :form-data="models" :this-div="thisDiv">
              <el-form-item slot="right" label="医疗介入" prop="medicalInvolvedArr">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.medicalInvolvedArr">
                    <el-checkbox v-for="(item, index) in medicalInterventionList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                  </el-checkbox-group>
                </div>
              </el-form-item>
              <div slot="right" style="width: 100%" />
              <el-form-item slot="right"  label="其他介入" prop="otherInvolvedArr">
                <div style="display: flex;">
                  <el-checkbox-group v-model="models.otherInvolvedArr">
                    <el-checkbox v-for="(item, index) in otherInterventionList" :key="index" :label="item.value"> {{ item.label }} </el-checkbox>
                    <el-input
                      v-if="models.otherInvolvedArr && models.otherInvolvedArr.indexOf('135')  > -1"
                      :style="{ width: '200px', 'margin-left': '5px' }"
                      placeholder="请输入"
                      v-model="models.baqsjRecordHandle.otherInvolvedOther"
                    />
                  </el-checkbox-group>
                </div>
              </el-form-item>
            </measure-two-info>
          </title-info>
        </div>
        <div >
          <title-info title="其他信息说明" >
            <other-info slot="left" :form-data="models" :this-div="thisDiv"/>
          </title-info>
        </div>
      </el-form>
    </div>
    <div v-if="thisDiv === 3">
      <div>
        <title-info title="上报⼈信息">
          <reporter-info slot="left" :form-data="models" :this-div="thisDiv" />
        </title-info>
      </div>
      <div>
        <title-info title="当事人信息">
          <party-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="当事患者信息">
          <patient-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="事件信息">
          <event-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
      <div>
        <title-info title="错误发⽣阶段">
          <div slot="left">
            <table>
              <tr>
                <td class="td-title">验⾎错误</td>
                <td>{{ getBloodTestErrors(models.bloodTestArr) || '' }}</td>
                <td class="td-title">备⾎错误</td>
                <td>{{ getBloodPrepareErrors(models.bloodPrepareArr) || '' }}</td>
              </tr>
              <tr>
                <td class="td-title">输⾎错误</td>
                <td colspan="3">{{ getBloodInputErrors(models.bloodInputArr) || '' }}</td>
              </tr>
            </table>
          </div>
        </title-info>
      </div>
      <div>
        <title-info title="事后引起的输⾎反应">
          <div slot="left">
            <table>
              <tr>
                <td class="td-title">反应类型</td>
                <td>{{ getBloodInputReactions(models.bloodInputReactionArr) || '' }}</td>
              </tr>
            </table>
          </div>
        </title-info>
      </div>
      <div>
        <title-info title="处理措施">
          <measure-two-info slot="left" :form-data="models" :this-div="thisDiv">
            <tr slot="right">
              <td class="td-title">医疗介入</td>
              <td>{{ formatMedicalInterventions(models.medicalInvolvedArr) || '' }}</td>
            </tr>
            <tr slot="right">
              <td class="td-title">其他介入</td>
              <td>{{ formatOtherInterventions(models.otherInvolvedArr) || '' }}</td>
            </tr>
          </measure-two-info>
        </title-info>
      </div>
      <div>
        <title-info title="其他信息说明">
          <other-info slot="left" :form-data="models" :this-div="thisDiv"/>
        </title-info>
      </div>
    </div>
  </div>
</template>

<script>
import { doSaveTransfusionWrongBlood } from '@/api/report/reportForm'

import titleInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/titleInfo.vue'
import reporterInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/reporterInfo.vue'
import partyInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/partyInfo.vue'
import patientInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/patientInfo.vue'
import eventInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/eventInfo.vue'
import measureTwoInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/measureTwoInfo.vue'
import otherInfo from '@/views/newBaqsj/apply/components/reportForm/dialog/otherInfo.vue'

export default {
  name: 'TransfusionWrongBlood',
  components: { titleInfo, reporterInfo, partyInfo, patientInfo, eventInfo, measureTwoInfo, otherInfo },
  data() {
    return {
      // 当前显示的div：1 dialog 2 详情 3 处置
      thisDiv: 0,
      models: {},
      loading: false,
      dialogVisible: false,
      dialogDetailVisible: false,
      submitLoading: false,
      saveLoading: false,
      rules: {},
      bloodTestList: [
        { value: '380', label: '⾎型错误' },
        { value: '381', label: '错误病⼈' },
        { value: '382', label: '其他' }
      ],
      bloodPrepareList: [
        { value: '390', label: '医嘱错误' },
        { value: '391', label: '数量与医嘱不同' },
        { value: '392', label: '电脑输⼊错误' },
        { value: '393', label: '时间延误' },
        { value: '394', label: '⾎制品保存不当' },
        { value: '395', label: '⾎制品遗失' },
        { value: '396', label: '⾎制品损坏' },
        { value: '397', label: '其他' }
      ],
      bloodInputList: [
        { value: '400', label: '⾎制品类型错误' },
        { value: '401', label: '⾎型错误' },
        { value: '402', label: '病⼈⾝份确认错误' },
        { value: '403', label: '技术错误' },
        { value: '404', label: '其他' }
      ],
      bloodInputReactionList: [
        { value: '410', label: '烦躁不安' },
        { value: '411', label: '⽪肤痒' },
        { value: '412', label: '⽪肤疹' },
        { value: '413', label: '⼼跳加速' },
        { value: '414', label: '呼吸急促' },
        { value: '415', label: '呼吸困难' },
        { value: '416', label: '发冷或寒颤' },
        { value: '417', label: '⾎尿' },
        { value: '418', label: '不明出⾎' },
        { value: '419', label: '⾎压下降' },
        { value: '420', label: '颜⾯潮红' },
        { value: '421', label: '发烧' },
        { value: '422', label: '疼痛' },
        { value: '423', label: '无' },
        { value: '424', label: '其他' }
      ],
      medicalInterventionList: [
        { value: '120', label: '持续观察' },
        { value: '121', label: '加强护理防范' },
        { value: '122', label: '监测生命体征' },
        { value: '123', label: '伤口护理' },
        { value: '124', label: '转诊或转院治疗' },
        { value: '125', label: '给予额外关注（如约束、注射治疗、药物治疗）' },
        { value: '126', label: '停⽌与事件有关的⽤药或治疗' },
        { value: '127', label: '急救' }
      ],
      otherInterventionList: [
        { value: '130', label: '予以病人家属慰问和支持' },
        { value: '131', label: '移除与危害相关的设备或环境运作' },
        { value: '132', label: '通报卫生主管机构' },
        { value: '133', label: '上报警察机关' },
        { value: '134', label: '医务⼈员之间进⾏沟通' },
        { value: '135', label: '其他' }
      ]
    }
  },
  methods: {
    handleClose(val) {
      this.dialogVisible = false
      // 传到父组件
      this.$emit('close', val)
    },
    handleDetailClose() {
      this.dialogDetailVisible = false
    },
    getDetail(info) {
      this.models = {
        ...info,
        medicalInvolvedArr: (info.baqsjRecordHandle && info.baqsjRecordHandle.medicalInvolved) ? info.baqsjRecordHandle.medicalInvolved.split(',') : [],
        otherInvolvedArr: (info.baqsjRecordHandle && info.baqsjRecordHandle.otherInvolved) ? info.baqsjRecordHandle.otherInvolved.split(',') : [],
        bloodTestArr: (info.baqsjRecordHandle && info.baqsjRecordHandle.bloodTest) ? info.baqsjRecordHandle.bloodTest.split(',') : [],
        bloodPrepareArr: (info.baqsjRecordHandle && info.baqsjRecordHandle.bloodPrepare) ? info.baqsjRecordHandle.bloodPrepare.split(',') : [],
        bloodInputArr: (info.baqsjRecordHandle && info.baqsjRecordHandle.bloodInput) ? info.baqsjRecordHandle.bloodInput.split(',') : [],
        bloodInputReactionArr: (info.baqsjRecordHandle && info.baqsjRecordHandle.bloodInputReaction) ? info.baqsjRecordHandle.bloodInputReaction.split(',') : []
      };
      console.log(this.models);
    },
    editInfo(ruleInfo, info) {
      this.thisDiv = 2
      this.dialogVisible = true
      this.rules = ruleInfo
      this.getDetail(info)
    },
    detail(info) {
      this.thisDiv = 3
      this.dialogDetailVisible = true
      this.getDetail(info)
    },
    open(ruleInfo, modelInfo) {
      // 弹出框
      this.thisDiv = 1
      this.rules = {
        ...ruleInfo
      }
      this.models = {
        ...modelInfo,
        eventNo: 'TransfusionWrongBlood',
        eventName: '输血相关—用血错误事件',
        // 其他复选框
        // 处理措施=>医疗介入
        medicalInvolvedArr: [],
        //  处理措施=>其他介入
        otherInvolvedArr: [],
        // 事件差错详情 => 对象类型(不同的表单，定制不同)
        baqsjRecordWrongblood: {},
        // 事件差错详情 =>  (不同的表单，定制不同)
        bloodTestArr: [],
        bloodPrepareArr: [],
        bloodInputArr: [],
        bloodInputReactionArr: []
      }
      this.dialogVisible = true
    },
    getBloodTestErrors(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.bloodTestList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '382') {
            labels.push(`其他（${this.models.baqsjRecordHandle.bloodTestOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    getBloodPrepareErrors(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.bloodPrepareList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '397') {
            labels.push(`其他（${this.models.baqsjRecordHandle.bloodPrepareOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    getBloodInputErrors(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.bloodInputList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '404') {
            labels.push(`其他（${this.models.baqsjRecordHandle.bloodInputOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    getBloodInputReactions(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.bloodInputReactionList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '424') {
            labels.push(`其他（${this.models.baqsjRecordHandle.bloodInputReactionOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    formatMedicalInterventions(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.medicalInterventionList.forEach(item => {
        labels.push(item.label);
      });
      return labels.join(', ');
    },
    formatOtherInterventions(values) {
      if (!values || values.length === 0) return '';
      const labels = [];
      this.otherInterventionList.forEach(item => {
        if (values.includes(item.value)) {
          if (item.value === '135') {
            labels.push(`其他（${this.models.baqsjRecordHandle.otherInvolvedOther || ''}）`);
          } else {
            labels.push(item.label);
          }
        }
      });
      return labels.join(', ');
    },
    submitAction() {
      // 上报
      this.models.auditStatus = 2
      this.actionRequest()
    },
    saveAction(item) {
      // 草稿
      this.models.auditStatus = 1
      this.actionRequest()
    },
    actionRequest() {
      // 新增、编辑、暂存
      this.$refs['generateForm'].validate(valid => {
        if (valid) {
          this.$message({
            message: '正在操作请稍等...',
            type: 'warning'
          })
          this.saveLoading = true
          this.submitLoading = true

          // 处理复选框数据
          this.models.baqsjRecordHandle.bloodTest = this.models.bloodTestArr.join(',');
          this.models.baqsjRecordHandle.bloodPrepare = this.models.bloodPrepareArr.join(',');
          this.models.baqsjRecordHandle.bloodInput = this.models.bloodInputArr.join(',');
          this.models.baqsjRecordHandle.bloodInputReaction = this.models.bloodInputReactionArr.join(',');
          doSaveTransfusionWrongBlood(this.models).then(res => {
            if (res.resStatus !== 500) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.saveLoading = false
              this.submitLoading = false
              // 关闭弹出框
              this.dialogVisible = false
              this.handleClose(1)
            } else {
              this.$message({
                type: 'error',
                message: '操作失败!'
              })
              this.saveLoading = false
              this.submitLoading = false
            }
          })
        } else {
          this.$message({
            message: '请填写必填项',
            type: 'warning'
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.title {
  padding: 12px;
  border-bottom: 1px solid rgba(0, 0, 0, .1);
  position: sticky;
  top: 0;
  z-index: 2000;
  background-color: #fff;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
  table-layout: fixed;
}
td {
  border: 1px solid #ccc;
  padding: 10px;
  text-align: left;
  /* 调整行间距 */
  line-height: 1.6;
  /* 调整字体大小 */
  font-size: 18px;
  /* 调整字间距 */
  letter-spacing: 0.5px;
}

.td-title {
  width: 12%;
  background-color: #fff;
}
</style>
